package principal;

import edu.uci.ics.jung.graph.DirectedGraph;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import javax.swing.*;
import model.Aluno;

public class Exibir_Grafo extends javax.swing.JFrame {
    
    private ArrayList<Integer> lista_rm = new ArrayList<Integer>();
    
    private ArrayList<Aluno> alunos_rm = new ArrayList<Aluno>();
    private ArrayList<Aluno> alunos_nome = new ArrayList<Aluno>();
    
    private DirectedGraph<String, String> g = null;
    
    boolean grafo_rm = false;

    public Exibir_Grafo(ArrayList<Integer> lista_rm, ArrayList<Aluno> alunos_rm, ArrayList<Aluno> alunos_nome, DirectedGraph<String, String> g, boolean grafo_rm) {
        initComponents();
        this.setLocationRelativeTo(null);
        
        this.lista_rm = lista_rm;
        this.alunos_rm = alunos_rm;
        this.alunos_nome = alunos_nome;
        this.g = g;
        
        this.grafo_rm = grafo_rm;
        
        groupButton();
        setLabels();
        rdFRLayout.setSelected(true);
        
        txtTamx.addActionListener(listener);
        txtTamy.addActionListener(listener);
        
        keystrokes();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblLayout = new javax.swing.JLabel();
        rdKKLayout = new javax.swing.JRadioButton();
        rdCircleLayout = new javax.swing.JRadioButton();
        rdFRLayout = new javax.swing.JRadioButton();
        rdFRLayout2 = new javax.swing.JRadioButton();
        rdISOMLayout = new javax.swing.JRadioButton();
        rdSpringLayout = new javax.swing.JRadioButton();
        rdSpringLayout2 = new javax.swing.JRadioButton();
        lblTamx = new javax.swing.JLabel();
        txtTamx = new javax.swing.JTextField();
        lblTamy = new javax.swing.JLabel();
        txtTamy = new javax.swing.JTextField();
        btnVoltar = new javax.swing.JButton();
        btnExibir = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Exibir Grafo");

        lblLayout.setFont(new java.awt.Font("DejaVu Sans", 1, 14)); // NOI18N
        lblLayout.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblLayout.setText("Selecione o Layout:");

        rdKKLayout.setText("KKLayout");

        rdCircleLayout.setText("CircleLayout");

        rdFRLayout.setText("FRLayout");

        rdFRLayout2.setText("FRLayout2");

        rdISOMLayout.setText("ISOMLayout");

        rdSpringLayout.setText("SpringLayout");

        rdSpringLayout2.setText("SpringLayout2");

        lblTamx.setText("Tam. [X]:");

        txtTamx.setText("1200");

        lblTamy.setText("Tam. [Y]:");

        txtTamy.setText("1000");

        btnVoltar.setText("Voltar");
        btnVoltar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnVoltarActionPerformed(evt);
            }
        });

        btnExibir.setText("Exibir");
        btnExibir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnExibirActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(rdKKLayout)
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(rdCircleLayout)
                            .addComponent(rdFRLayout)
                            .addComponent(rdFRLayout2)
                            .addComponent(rdISOMLayout)
                            .addComponent(rdSpringLayout)
                            .addComponent(rdSpringLayout2)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(lblTamx)
                                    .addComponent(lblTamy))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(txtTamx)
                                    .addComponent(txtTamy)))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(btnVoltar, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btnExibir, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(lblLayout))
                        .addGap(0, 14, Short.MAX_VALUE))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblLayout)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(rdKKLayout)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(rdCircleLayout)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(rdFRLayout)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(rdFRLayout2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(rdISOMLayout)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(rdSpringLayout)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(rdSpringLayout2)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblTamx)
                    .addComponent(txtTamx, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtTamy, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblTamy))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnVoltar)
                    .addComponent(btnExibir))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnVoltarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnVoltarActionPerformed
        voltar();
    }//GEN-LAST:event_btnVoltarActionPerformed

    private void btnExibirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnExibirActionPerformed
        exibir();
    }//GEN-LAST:event_btnExibirActionPerformed

    private void keystrokes() {
        KeyStroke escapeKeyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0, false);
        Action escapeAction = new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
               voltar();
            }  
        };
        
        this.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(escapeKeyStroke, "ESCAPE");
        this.getRootPane().getActionMap().put("ESCAPE", escapeAction);
    }
    
    private ActionListener listener = new ActionListener() {  
        public void actionPerformed(ActionEvent evt) {  
            exibir();
        }  
    };
    
    private void groupButton() {
        ButtonGroup bg1 = new ButtonGroup();
        bg1.add(rdKKLayout);
        bg1.add(rdCircleLayout);
        bg1.add(rdFRLayout);
        bg1.add(rdFRLayout2);
        bg1.add(rdISOMLayout);
        bg1.add(rdSpringLayout);
        bg1.add(rdSpringLayout2);
    }
    
    private void setLabels() {
        txtTamx.setText("600");
        txtTamy.setText("600");
    }
    
    private void exibir() {
        try {
            Integer tamx = Integer.parseInt(txtTamx.getText());
            Integer tamy = Integer.parseInt(txtTamy.getText());
            
            if(tamx < 600 || tamy < 600) {
                JOptionPane.showMessageDialog(null, "Digite valores maiores que 600!", "Aviso", JOptionPane.WARNING_MESSAGE);
                txtTamx.setText("600");
                txtTamy.setText("600");
            }
            else {
                String layout_tipo = "";
                if(rdCircleLayout.isSelected()) {
                    layout_tipo = "CircleLayout";
                }
                else if (rdFRLayout.isSelected()) {
                    layout_tipo = "FRLayout";
                }
                else if (rdFRLayout2.isSelected()) {
                    layout_tipo = "FRLayout2";
                }
                else if (rdISOMLayout.isSelected()) {
                    layout_tipo = "ISOMLayout";
                }
                else if (rdSpringLayout.isSelected()) {
                    layout_tipo = "SpringLayout";
                }
                else if (rdSpringLayout2.isSelected()) {
                    layout_tipo = "SpringLayout2";
                }
                Grafo gr = new Grafo();
                gr.exibir_grafo(g, layout_tipo, tamx, tamy);
            }
        }
        catch(NumberFormatException e) {
            JOptionPane.showMessageDialog(null, "Digite números válidos inteiros nas dimensões!", "Aviso", JOptionPane.WARNING_MESSAGE);
            txtTamx.setText("600");
            txtTamy.setText("600");
        }
            
    }
    
    private void voltar() {
        boolean verificar = false;
        if (lista_rm != null && lista_rm.size() > 0) {
            if (alunos_rm != null && alunos_rm.size() > 0) {
                if (alunos_nome != null && alunos_nome.size() > 0) {
                    if(g != null) {
                        verificar = true;
                    }
                }
            }
        }
        if (verificar) {
            Principal jn = new Principal(lista_rm, alunos_rm, alunos_nome, g, grafo_rm);
            jn.setVisible(true);
            this.dispose();
        } else {
            Principal jn = new Principal();
            jn.setVisible(true);
            this.dispose();
        }
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnExibir;
    private javax.swing.JButton btnVoltar;
    private javax.swing.JLabel lblLayout;
    private javax.swing.JLabel lblTamx;
    private javax.swing.JLabel lblTamy;
    private javax.swing.JRadioButton rdCircleLayout;
    private javax.swing.JRadioButton rdFRLayout;
    private javax.swing.JRadioButton rdFRLayout2;
    private javax.swing.JRadioButton rdISOMLayout;
    private javax.swing.JRadioButton rdKKLayout;
    private javax.swing.JRadioButton rdSpringLayout;
    private javax.swing.JRadioButton rdSpringLayout2;
    private javax.swing.JTextField txtTamx;
    private javax.swing.JTextField txtTamy;
    // End of variables declaration//GEN-END:variables
}
